package com.itheima.freemarker.controller;


import com.itheima.freemarker.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hello")
public class HelloControler {
    /**
     * 快速入门
     * modelAndView
     * model
     * modelMap
     * @return
     */
    @GetMapping
    public String hello(Model model){
        //封装数据
        String name="二狗子";
        model.addAttribute("name",name);
        model.addAttribute("age",12);
        //封装对象
        Student student=new Student();
        student.setName("韩梅梅");
        student.setAge(16);
        model.addAttribute("student",student);
        //传输并跳转页面
        return "01-basic";
    }

    /**
     * 快速入门
     * modelAndView
     * model
     * modelMap
     * @return
     */
    @GetMapping("/modelMap")
    public String hello(ModelMap model){
        //封装数据
        String name="二狗子";
        model.addAttribute("name",name);
        model.put("age",12);
        //传输并跳转页面
        return "01-basic";
    }

    /**
     * 快速入门
     * modelAndView 模型和视图
     * model
     * modelMap
     * @return
     */
    @GetMapping("/mav")
    public ModelAndView hello(){
        ModelAndView mav=new ModelAndView();
        //封装数据
        mav.addObject("name","王五");
        mav.addObject("age",15);
        //跳转页面
        mav.setViewName("01-basic");
        return mav;
    }
    /**
     * 集合遍历
     */
    @GetMapping("/list")
    public String index(Model model){
     //list类型
        //封装数据
        Student student=new Student();
        student.setName("韩梅梅");
        student.setAge(16);
        student.setMoney(12D);

        Student student2=new Student();
        student2.setName("李雷");
        student2.setAge(15);
        student2.setMoney(13D);
        List<Student> studentList=new ArrayList<>();
        studentList.add(student);
        studentList.add(student2);
        model.addAttribute("stuList",studentList);
   //map类型
        Map<String,Student> map=new HashMap<>();
        map.put("stu1",student);
        map.put("stu2",student2);

        model.addAttribute("stuMap",map);

        //传输并跳转页面
        return "02-list";
    }

}
